package com.fanghaoran.view;

import com.fanghaoran.bean.Interaction;
import com.fanghaoran.controller.UserController;
import com.fanghaoran.view.user.BackGroundPanel;
import com.fanghaoran.util.CodeUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;

/**
 * @Author: win11
 * @Date: 2023/03/16/13:36
 * @Description: 注册页面
 */
public class RegisterFrame extends JFrame {
    public RegisterFrame() throws Exception {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView();

        //设置可见
        this.setVisible(true);
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(400, 400);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void initView() throws IOException {
        //panel来放组件
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //vbox整体布局
        Box vbox = Box.createVerticalBox();
        //水平排列的box 完成用户名及对应文本框
        Box aBox = Box.createHorizontalBox();
        //用户名文本
        JLabel usernameText = new JLabel("用户名:");
        //文本输入框
        JTextField username = new JTextField(15);
        //添加到box
        aBox.add(usernameText);
        //给一些水平距离 不会凑在一块
        aBox.add(Box.createHorizontalStrut(20));
        aBox.add(username);

        //水平排列的box 完成密码及对应文本框
        Box pBox = Box.createHorizontalBox();
        //密码文本
        JLabel passwordText = new JLabel("密    码:");
        //密码文本输入框
        JTextField password = new JTextField(15);
        //添加到box
        pBox.add(passwordText);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(password);

        //水平排列的box 完成手机号及对应文本框
        Box tBox = Box.createHorizontalBox();
        //手机号文本
        JLabel teleText = new JLabel("手机号:");
        //手机号文本输入框
        JTextField tele = new JTextField(15);
        //添加到box
        tBox.add(teleText);
        tBox.add(Box.createHorizontalStrut(20));
        tBox.add(tele);

        //水平box 完成验证码相关操作
        Box cBox = Box.createHorizontalBox();
        //验证码文本
        JLabel codeText = new JLabel("验证码:");
        //验证码输入框
        JTextField code = new JTextField(10);
        //正确验证码的框
        //JLabel rightCode=new JLabel();

        //工具类生成验证码
        String rCode = CodeUtil.getCode();
        //验证码按钮
        JButton rightCode = new JButton(rCode);
        rightCode.setSize(50, 50);
        //添加到box
        cBox.add(codeText);
        cBox.add(Box.createHorizontalStrut(20));
        cBox.add(code);
        cBox.add(Box.createHorizontalStrut(20));
        cBox.add(rightCode);

        //组装按钮
        //水平box存放按钮
        Box bbox = Box.createHorizontalBox();
        //注册按钮
        JButton registerButton = new JButton("注册");
        bbox.add(registerButton);

        //调整竖直box的位置,使其大致居中
        vbox.add(Box.createVerticalStrut(100));
        //水平box放到vbox中并添加上下距离
        vbox.add(aBox);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(pBox);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(tBox);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(cBox);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(bbox);

        //vbox添加到panel
        panel.add(vbox);
        //panel添加到frame
        this.add(panel);

        //给组件添加点击事件
        //注册
        registerButton.addActionListener((e) -> {
                    //获取文本输入框数据
                    //用户名
                    String usernameInput = username.getText();
                    //密码
                    String passwordInput = password.getText();
                    //手机号
                    String teleInput = tele.getText();
                    //验证码
                    String codeInput = code.getText();

                    //用户输入传到controller控制层
                    //controller层返回执行结果
                    Interaction register = null;
                    try {
                        register = new UserController().register(usernameInput, passwordInput, teleInput, codeInput, rightCode.getText());
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                    //根据结果进行页面操作
                    if (register != null) {
                        if ("用户名为空".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "用户名不能为空!");
                        } else if ("密码为空".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "密码不能为空!");
                        } else if ("手机号为空".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "手机号不能为空!");
                        } else if ("验证码为空".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "验证码不能为空!");
                        } else if ("密码不符合规范".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "密码不符合规范!");
                        } else if ("手机号无效".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "手机号无效!");
                        } else if ("验证码错误".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "验证码错误!");
                        } else if ("用户名已存在".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "用户名已存在!");
                        } else if ("该手机号已绑定其他账号".equals(register.getResult())) {
                            JOptionPane.showMessageDialog(panel, "该手机号已绑定其他账号!");
                        }
                        //校验通过
                        else {
                            JOptionPane.showMessageDialog(panel, "注册成功!");
                            //关闭当前页面
                            this.dispose();
                            //跳转回登录界面
                            try {
                                new LoginFrame();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
        );

        //验证码(点击更换)
        rightCode.addActionListener((e) -> {
                    System.out.println("更换验证码");
                    String newCode = CodeUtil.getCode();
                    rightCode.setText(newCode);
                }
        );

    }
}
